package com.shenhaofeng.app.utils.intent;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import java.io.File;
import java.util.List;


/**
 * 一些常用的系统Intent
 * Created by Administrator on 2016/8/29.
 */
public class Intents {


    /**
     * 获取拨号Intent
     *
     * @return
     */
    public static Intent getDialIntent(String tel) {
        return new Intent(Intent.ACTION_DIAL, Uri.parse(String.format("tel:%s", tel)));
    }


    /**
     * 跳转网页
     *
     * @param url
     * @return
     */
    public static Intent getWebIntent(String url) {
        return new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("http://%s", url)));
    }


    /**
     * 安装应用
     * @param apkFile
     * @return
     */
    public static Intent getInstallIntent(File apkFile) {
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        String mime = "application/vnd.android.package-archive";
        intent.setDataAndType(Uri.fromFile(apkFile), mime);
        return intent;
    }


    /**
     * 获取搜索的Intent
     *
     * @param searchString
     * @return
     */
    public static Intent getSearchIntent(String searchString) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_WEB_SEARCH);
        intent.putExtra(SearchManager.QUERY, searchString);
        return intent;
    }


    /**
     * 获取播放视频的intent
     *
     * @param uri
     * @return
     */
    public static Intent getPlayVideo(Uri uri) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "video/*");
        return intent;
    }


    /**
     * 获取播放硬盘的intent
     *
     * @param uri
     * @return
     */
    public static Intent getPlayAudio(Uri uri) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "audio/*");
        return intent;
    }


    /**
     * @return
     */
    public static Intent getMapIntent() {
        Uri uri = Uri.parse("geo:38.899533,-77.036476");
        return new Intent(Intent.ACTION_VIEW, uri);
    }


    /**
     * 调起短信程序
     *
     * @param message
     * @return
     */
    public static Intent getSMSIntent(String message) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.putExtra("sms_body", message);
        intent.setType("vnd.android-dir/mms-sms");
        return intent;
    }


    /**
     * 检查指定的intent是否能找到
     *
     * @param intent
     * @return
     */
    public static boolean checkIntentActivity(Context context, Intent intent) {
        final PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }


}
